package com.shuyuntu.proposal.controller;

import com.shuyuntu.proposal.entity.Message;
import com.shuyuntu.proposal.security.UseSecurity;
import com.shuyuntu.proposal.service.MessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Message的控制器类
 *
 * 主要实现消息的查询、计数等
 * @author limeng
 * Created by limeng on 16-4-26.
 */
@Controller
@RequestMapping("/messages")
public class MessageController {

    /**
     * 获取用户的未读信息数
     *
     * @return 未读信息数
     */
    @ResponseBody
    @RequestMapping(value = "/new/count", method = RequestMethod.GET)
    @UseSecurity(category = "05")
    public Integer getNewMessagesCount(HttpSession session) {
        String userId = (String)session.getAttribute("loginUserId");
        if (userId == null) {
            return 0;
        }

        Integer count = messageService.getUnreadMessagesCount(userId);
        return count;
    }

    /**
     * 获取最新消息列表
     * @param count 获取的条数
     * @param session injection
     * @return json-list
     */
    @ResponseBody
    @RequestMapping(value = "/new/list/{count}", method = RequestMethod.GET)
    @UseSecurity(category = "05")
    public List<Message> getNewMessagesList(@PathVariable int count, HttpSession session) {
        String userId = (String)session.getAttribute("loginUserId");
        if (userId == null) {
            return null;
        }

        List<Message> list = messageService.getUnreadMessagesList(userId, count);
        return list;
    }

    /**
     * 分页获取消息
     * @param pageSize 页长
     * @param pageNo 页码
     * @return json-map
     */
    @ResponseBody
    @UseSecurity(category = "05")
    @RequestMapping(value = "/pagination/{pageSize}/{pageNo}")
    public Map<String, Object> getMessagesWithPagination(
            @PathVariable int pageSize,
            @PathVariable int pageNo,
            HttpSession session) {
        Map<String, Object> resultMap = new LinkedHashMap<>();
        String userId = (String) session.getAttribute("loginUserId");
        List messageList = messageService.getMessagesWithPagination(pageSize, pageNo, userId);
        Integer count = messageService.getMessagesCount(userId);

        int pageCount = (count - 1) / pageSize + 1;

        resultMap.put("messages", messageList);
        resultMap.put("pageCount", pageCount);

        return resultMap;
    }

    /**
     * 将某条消息设为已读
     * @param messageId 消息的id
     * @param session injection
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/setRead/{messageId}")
    @UseSecurity(category = "05")
    public Map<String, Object> setRead(
            @PathVariable Integer messageId,
            HttpSession session) {
        String loginUserId = (String)session.getAttribute("loginUserId");
        Map<String, Object> resMap = new LinkedHashMap<>();
        int result = messageService.setRead(messageId, loginUserId);
        switch (result) {
            case 0:
                resMap.put("state", true);
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "没有找到指定id的message");
                break;
            case 2:
                resMap.put("state", false);
                resMap.put("result", "没有相关权限");
                break;
            case -1:
                resMap.put("state", false);
                resMap.put("result", "数据库操作异常");
                break;
        }
        return resMap;
    }

    @Resource
    MessageService messageService;

    public MessageService getMessageService() {
        return messageService;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }
}
